﻿using System;
using System.Threading.Tasks;
using PubSub.S3.Abstract.LocalEventBus;

namespace PubSub.S3
{
    /// <summary>
    /// Action执行工厂
    /// </summary>
    /// <remarks>
    /// 订阅者注册Action的执行代码
    /// </remarks>
    /// <typeparam name="TEvent"></typeparam>
    public class ActionEventHandler<TEvent> : ILocalEventHandler<TEvent>
    {
        /// <summary>
        /// Function to handle the event.
        /// </summary>
        public Func<TEvent, Task> Action { get; }

        /// <summary>
        /// Creates a new instance of <see cref="ActionEventHandler{TEvent}"/>.
        /// </summary>
        /// <param name="handler">Action to handle the event</param>
        public ActionEventHandler(Func<TEvent, Task> handler)
        {
            Action = handler;
        }

        /// <summary>
        /// Handles the event.
        /// </summary>
        /// <param name="eventData"></param>
        public async Task HandleEventAsync(TEvent eventData)
        {
            await Action(eventData);
        }
    }
}